package com.ocom.wxapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MapKey;
import com.ocom.common.request.wx.UserPageUserListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.wxapi.client.ExposeApiFeignService;
import com.ocom.wxapi.client.HumanClientService;
import com.ocom.wxapi.service.UserPageService;
import com.ocom.wxapi.vo.UserInfoMapVo;
import com.ocom.wxapi.vo.UserInfoYkmVo;
import com.ocom.wxapi.vo.UserListVo;
import com.ocom.wxapi.vo.userpage.PullFYInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserPageServiceImpl implements UserPageService {

//    @Autowired
//    private EmqxClientService emqxClientService;

    @Autowired
    private ExposeApiFeignService exposeApiFeignService;

    @Autowired
    HumanClientService humanClientService;

    @Override
    public Result userList(UserPageUserListRequest request) {
        Map<String, Object> map1 = new HashMap<>();
        MapKey mapKey = new MapKey();
        map1.put(mapKey.getKey_comId(),request.getCompanyId());
        map1.put(mapKey.getKey_deptId(), request.getDeptId());
//        map1.put(mapKey.getKey_pId(),request.getPId());
        map1.put(mapKey.getKey_page(),0);
        map1.put(mapKey.getKey_size(),0);
        Result result= humanClientService.PersonnelGetPersonInfo(map1);

        List<UserListVo> list = new ArrayList<>();
        if(result.getCode()==200) {
            UserInfoMapVo userInfoMapVo = JSON.parseObject(JSON.toJSONString(result.getData()), UserInfoMapVo.class);
            List<UserInfoYkmVo> list1 = userInfoMapVo.getList();
            if (list1.size() > 0){
                for (UserInfoYkmVo userInfoYkmVo : list1) {
                    UserListVo userListVo = new UserListVo();
                    userListVo.setPId(userInfoYkmVo.getPId());
                    userListVo.setPName(userInfoYkmVo.getPName());
                    userListVo.setTelephone(userInfoYkmVo.getPPhone());
//                    userListVo.setCodeColor(userInfoYkmVo.getCodeColor());
//                    userListVo.setColorTime(userInfoYkmVo.getColorTime());
//                    userListVo.setHsResult(userInfoYkmVo.getHsResult());
//                    userListVo.setHsTime(userInfoYkmVo.getHsTime());
//                    userListVo.setYmNum(userInfoYkmVo.getYmNum());
//                    userListVo.setYmTime(userInfoYkmVo.getYmTime());
                    list.add(userListVo);
                }
            }

        }
        return ResultUtil.success(list);
    }

    /**
     * 拉取防疫信息（UserPage/pullFYInfo ）
     * */
    @Override
    public Result pullFYInfo(PullFYInfoVo pullFYInfoVo) {

//        Result result = humanClientService.GetPersonInfo(pullFYInfoVo.getComId(),pullFYInfoVo.getPId());
//        if (result.getCode() != 200) {
//            return ResultUtil.error(ResultEnum.Err_311);
//        } else {
//            Gson gson = new Gson();
//            UserInfoYkmVo userInfoYkmVo = gson.fromJson(gson.toJson(result.getData()), UserInfoYkmVo.class);
//            if (!IdentificationCodeUtil.isIdentityCode(userInfoYkmVo.getPIdentity())) {
//                return ResultUtil.error(ResultEnum.Err_2021);
//            }
//            FyRequest fyRequest = new FyRequest();
//            fyRequest.setComid(Math.toIntExact(pullFYInfoVo.getComId()));
//            fyRequest.setParam(userInfoYkmVo.getPIdentity());
//            fyRequest.setTimestamp(new Date().getTime());
//            fyRequest.setName(userInfoYkmVo.getPName());
//            fyRequest.setNonceStr(RandomUtil.getRandomString(48));
//            fyRequest.setTransferId(RandomUtil.getRandomString(42));
//            fyRequest.setType(1);
//
////            Result fangyi = emqxClientService.fangyi(fyRequest);
//
//            Result fangyi=exposeApiFeignService.insideFangYi(1,pullFYInfoVo.getComId().intValue(),userInfoYkmVo.getPIdentity(),userInfoYkmVo.getPName());
//            if (fangyi.getCode()!=200){
//                ResultUtil.error(fangyi.getCode());
//            }
            return ResultUtil.success();
//
//        }
    }
}
